package com.fz.service.system.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fz.common.ResponseEntity;
import com.fz.dto.system.user.UserAdd;
import com.fz.dto.system.user.UserQuery;
import com.fz.dto.system.user.UserUpd;
import com.fz.entity.system.Menu;
import com.fz.entity.system.User;
import com.fz.mapper.system.UserMapper;
import com.fz.service.system.UserService;
import com.fz.utils.JacksonUtils;
import com.fz.utils.StringUtils;
import com.fz.vo.system.user.UserVo;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
@SuppressWarnings("unchecked")
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    /**
     * @方法描述: 根据登录名查询用户信息
     * @参数:
     * @返回值:
     * @创建人: I am God
     * @创建时间:: 2021/3/17
     */
    public User findUserByLoginid(String loginid) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(User::getLoginId, loginid);
        return userMapper.selectOne(lambdaQueryWrapper);
    }


    /**
     * @方法描述: 查询用户所拥有的菜单权限，并返回  GrantedAuthority
     * @参数:
     * @返回值:
     * @创建人: I am God
     * @创建时间:: 2021/3/17
     */
    public Collection<GrantedAuthority> mapToRoleAuthoritiesMenu(String userId) {
        List<Menu> menus = userMapper.findMenuPermission(userId);
        Set<String> permissions = menus.stream().map(Menu::getPermission).filter(StringUtils::isNotEmpty).collect(Collectors.toSet());
        return permissions.stream().map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
    }


    @Override
    public ResponseEntity<UserVo> findUserList(Integer pageSize, Integer pageNum, UserQuery userQuery) throws Exception{
        Page<UserVo> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ObjectUtil.isNotNull(userQuery.getDeptId()) , User::getDeptId, userQuery.getDeptId())
                .like(StringUtils.isNotEmpty(userQuery.getEmail()), User::getEmail, userQuery.getEmail())
                .like(StringUtils.isNotEmpty(userQuery.getPhone()), User::getPhone, userQuery.getPhone())
                .eq(ObjectUtil.isNotNull(userQuery.getStatus()), User::getStatus, userQuery.getStatus())
                .eq(StringUtils.isNotEmpty(userQuery.getSex()), User::getSex, userQuery.getSex())
                .orderByDesc(User::getCreateTime);
        Page<UserVo> loginLogList = userMapper.findUserList(page, lambdaQueryWrapper);
        return ResponseEntity.buildSuccess(loginLogList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity updateUser(UserUpd userUpd) throws Exception {
        User user = JacksonUtils.obj2pojo(userUpd, User.class);
        userMapper.updateById(user);
        return ResponseEntity.buildSuccess();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity saveUser(UserAdd userAdd) throws Exception{
        User user = JacksonUtils.obj2pojo(userAdd, User.class);
        userMapper.insert(user);
        return ResponseEntity.buildSuccess();
    }

    @Override
    public ResponseEntity delUser(String ids) throws Exception {
        Integer[] integers = Convert.toIntArray(ids);
        userMapper.deleteBatchIds(Arrays.asList(integers));
        return ResponseEntity.buildSuccess();
    }

    @Override
    public ResponseEntity<UserVo> selectUsersList(UserQuery userQuery) throws Exception {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ObjectUtil.isNotNull(userQuery.getDeptId()) , User::getDeptId, userQuery.getDeptId())
                .like(StringUtils.isNotEmpty(userQuery.getEmail()), User::getEmail, userQuery.getEmail())
                .like(StringUtils.isNotEmpty(userQuery.getPhone()), User::getPhone, userQuery.getPhone())
                .eq(ObjectUtil.isNotNull(userQuery.getStatus()), User::getStatus, userQuery.getStatus())
                .eq(StringUtils.isNotEmpty(userQuery.getSex()), User::getSex, userQuery.getSex())
                .orderByDesc(User::getCreateTime);
        return ResponseEntity.buildSuccess(userMapper.selectUsersList(lambdaQueryWrapper));
    }
}
